package cn.dengta.webapp.user.entity;

import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Nonnull;

import cn.dengta.common.model.Status;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.user.model.GiftType;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.X;

/**
 * 奖品信息
 *
 * @author lgy
 * @since 2022-08-10
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("dt_prize")
public class Prize extends BizEntity {

	/** 奖品名称 */
	private String name;
	/** 活动ID */
	private Long activityId;
	/** 是否虚拟商品 */
	private Integer isVirtual;
	/** 礼品类型 */
	private GiftType giftType;
	/** 礼品ID */
	private Long giftId;
	/** 礼物图 */
	private String giftIcon;
	/** 价值（单位：元） */
	private Integer amount;
	/** 奖品总数量 */
	private Integer totalCount;
	/** 抽奖概率权重系数 */
	private Integer factor;
	/** 最小出现区间 */
	private Integer minTimes;
	/** 最大出现区间 */
	private Integer maxTimes;
	/** 已中奖数量（所有） */
	private Integer usedCount;
	/** 已中奖数量（免费钻） */
	private Integer usedFreeCount;
	/** 已中奖数量（免费次数） */
	private Integer usedFreeTimes;
	/** 交易总额度（所有） */
	protected BigDecimal tradeTotalAmount;
	/** 本次交易使用的免费钻石 */
	protected BigDecimal tradeTotalFreePart;
	/** 获奖礼物总价值 */
	protected Integer totalAmount;
	/** 奖品等级 */
	private Integer level;
	/** 添加用户ID */
	private Long addUserId;

	public Prize init(Prize input, Date now) {
		this.activityId = X.expectNotNull(activityId, input.activityId);
		this.name = X.expectNotNull(input.name, name);
		this.giftIcon = X.expectNotNull(input.giftIcon, giftIcon);
		this.isVirtual = X.expectNotNull(input.isVirtual, isVirtual, 1);
		this.amount = X.expectNotNull(input.amount, amount);
		this.giftType = X.expectNotNull(input.giftType, giftType);
		this.giftId = X.expectNotNull(input.giftId, giftId);
		this.totalCount = X.expectNotNull(input.totalCount, totalCount);
		this.factor = X.expectNotNull(input.factor, 0);
		this.usedCount = X.expectNotNull(usedCount, 0);
		this.usedFreeCount = X.expectNotNull(usedFreeCount, 0);
		this.totalAmount = X.expectNotNull(totalAmount, 0);
		this.tradeTotalAmount = X.expectNotNull(tradeTotalAmount, BigDecimal.ZERO);
		this.tradeTotalFreePart = X.expectNotNull(tradeTotalFreePart, BigDecimal.ZERO);
		this.level = X.expectNotNull(input.level, level);
		this.minTimes = input.minTimes;
		this.maxTimes = input.maxTimes;
		super.initActiveStatus();
		super.initTime(now);
		return this;
	}

	public boolean virtual() {
		return Status.YES.eq(isVirtual);
	}

	public Integer addUsedCount(boolean free) {
		if (free) {
			usedFreeCount++;
		}
		totalAmount += amount;
		return ++usedCount;
	}

	public BigDecimal addTradeAmount(@Nonnull BigDecimal prize, boolean free) {
		if (free) {
			tradeTotalFreePart = tradeTotalFreePart.add(prize);
		}
		return tradeTotalAmount = tradeTotalAmount.add(prize);
	}

	public boolean hasRemain() {
		return totalCount > usedCount;
	}

	public int remainCount() {
		return totalCount - usedCount;
	}

	public static final String NAME = "name";
	public static final String GIFT_ICON = "gift_icon";
	public static final String ACTIVITY_ID = "activity_id";
	public static final String IS_VIRTUAL = "is_virtual";
	public static final String GIFT_TYPE = "gift_type";
	public static final String GIFT_ID = "gift_id";
	public static final String AMOUNT = "amount";
	public static final String TOTAL_AMOUNT = "total_amount";
	public static final String TOTAL_COUNT = "total_count";
	public static final String FACTOR = "factor";
	public static final String USED_COUNT = "used_count";
	public static final String USED_FREE_COUNT = "used_free_count";
	public static final String USED_FREE_TIMES = "used_free_times";
	public static final String TRADE_TOTAL_AMOUNT = "trade_total_amount";
	public static final String TRADE_TOTAL_FREE_PART = "trade_total_free_part";
	public static final String LEVEL = "level";
	public static final String MIN_TIMES = "min_times";
	public static final String MAX_TIMES = "max_times";
	public static final String ADD_USER_ID = "add_user_id";

}
